package lib.quasar.widget.edit;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.AppCompatEditText;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputConnectionWrapper;
import lib.quasar.util.LogUtil;
import lib.quasar.widget.R;

/**
 * description: 有清除功能,只有当有焦点和文本不为空的时候才显示
 * created by kalu on 17-10-17 下午12:10
 */
public class PhoneEditText extends AppCompatEditText {

    // 背景颜色
    private int backgroundColor;
    // 背景圆角
    private float backgroundCorners;
    // 背景边框颜色
    private int backgroundStoreColor;
    // 背景边框宽度
    private float backgroundStoreSize;

    private final GradientDrawable bg = new GradientDrawable();

    public PhoneEditText(Context context) {
        super(context);
    }

    public PhoneEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = null;
        try {
            typedArray = context.obtainStyledAttributes(attrs, R.styleable.PhoneEditText, 0, 0);
            backgroundColor = typedArray.getColor(R.styleable.PhoneEditText_pet_background_color, Color.TRANSPARENT);
            backgroundStoreColor = typedArray.getColor(R.styleable.PhoneEditText_pet_background_store_color, Color.TRANSPARENT);
            backgroundCorners = typedArray.getDimension(R.styleable.PhoneEditText_pet_background_corners, 0);
            backgroundStoreSize = typedArray.getDimension(R.styleable.PhoneEditText_pet_background_store_size, 0);
        } catch (Exception e) {
            LogUtil.e("", e.getMessage(), e);
        } finally {
            if (null == typedArray) return;
            typedArray.recycle();
        }
    }

    public PhoneEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /*****************************************************************************/

    @Override
    protected void onDraw(Canvas canvas) {

        // 圆角矩形背景
        bg.setCornerRadius(backgroundCorners);
        bg.setColor(backgroundColor);
        bg.setSize(getWidth(), getHeight());
        bg.setStroke((int) backgroundStoreSize, backgroundStoreColor);
        setBackgroundDrawable(bg);

        super.onDraw(canvas);
    }

    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        return new PhoneEditText.InputWrapper(super.onCreateInputConnection(outAttrs), false);
    }

    private final class InputWrapper extends InputConnectionWrapper implements InputConnection {

        public InputWrapper(InputConnection target, boolean mutable) {
            super(target, mutable);
        }

        @Override
        public boolean commitText(CharSequence text, int newCursorPosition) {
            if (!text.toString().matches("[0-9]+")) return false;
            return super.commitText(text, newCursorPosition);
        }

        @Override
        public boolean sendKeyEvent(KeyEvent event) {
            return super.sendKeyEvent(event);
        }

        @Override
        public boolean setSelection(int start, int end) {
            return super.setSelection(start, end);
        }
    }
}